package com.gcspring.h2db.config;

import com.gcspring.h2db.entity.DeviceIdMapper;
import com.gcspring.h2db.repo.DeviceIdMapperRepository;
import com.gcspring.h2db.utils.CodeRuleUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: guc
 * @Description: Ape设备id自动配置
 * @Date: 2022-9-2 10:45
 * @Version: 1.0
 */
@Component
public class ApeRunner implements ApplicationRunner {
    @Autowired
    private DataConfig dataConfig;
    @Autowired
    private ApeConfig apeConfig;
    @Autowired
    private Server1400Config server1400Config;
    @Autowired
    private DeviceIdMapperRepository deviceIdMapperRepository;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (dataConfig.getEnable()) { //开启数据库抽取
            List<DeviceIdMapper> mappers = deviceIdMapperRepository.findAll();
            List<String> zpxj = apeConfig.getZpxj(); //抓拍相机
            String sbbm;
            DeviceIdMapper deviceIdMapper;
            for (int i = 0; i < zpxj.size(); i++) {
                sbbm = CodeRuleUtils.getDeviceId(server1400Config.getCenterNumber(), CodeRuleUtils.TYPE_RXZP, i);
                deviceIdMapper = new DeviceIdMapper();
                deviceIdMapper.setSbIdDest(sbbm);
                deviceIdMapper.setSbIdSrc(zpxj.get(i));
                deviceIdMapper.setType(CodeRuleUtils.TYPE_RXZP);
                if (!isExist(mappers, deviceIdMapper)) {
                    mappers.add(deviceIdMapper);
                }
            }
            List<String> dz = apeConfig.getDz(); //道闸
            for (int i = 0; i < dz.size(); i++) {
                sbbm = CodeRuleUtils.getDeviceId(server1400Config.getCenterNumber(), CodeRuleUtils.TYPE_CLZP, i);
                deviceIdMapper = new DeviceIdMapper();
                deviceIdMapper.setSbIdDest(sbbm);
                deviceIdMapper.setSbIdSrc(dz.get(i));
                deviceIdMapper.setType(CodeRuleUtils.TYPE_CLZP);
                if (!isExist(mappers, deviceIdMapper))
                    mappers.add(deviceIdMapper);
            }
            List<String> mj = apeConfig.getMj(); //门禁
            for (int i = 0; i < mj.size(); i++) {
                sbbm = CodeRuleUtils.getDeviceId(server1400Config.getCenterNumber(), CodeRuleUtils.TYPE_MJSB, i);
                deviceIdMapper = new DeviceIdMapper();
                deviceIdMapper.setSbIdDest(sbbm);
                deviceIdMapper.setSbIdSrc(dz.get(i));
                deviceIdMapper.setType(CodeRuleUtils.TYPE_MJSB);
                if (!isExist(mappers, deviceIdMapper))
                    mappers.add(deviceIdMapper);
            }
            deviceIdMapperRepository.saveAll(mappers);
        } else {
            deviceIdMapperRepository.deleteAll();
        }

    }

    private boolean isExist(List<DeviceIdMapper> sources, DeviceIdMapper idMapper) {
        for (int i = 0; i < sources.size(); i++) {
            if (idMapper.getSbIdSrc().equals(sources.get(i).getSbIdSrc()) && idMapper.getType().equals(sources.get(i).getType())) {
                return true;
            }
        }
        return false;
    }
}
